<template>
  <div class="tree-container">
    <!--查询表单-->
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item label="一级课程分类：">
        <el-input
          clearable
          maxlength="20"
          v-model="searchParams.name"
          show-word-limit/>
      </el-form-item>

      <el-button type="primary" icon="el-icon-search" @click="fetchData()" size="medium">查询</el-button>
      <el-button type="warning" icon="el-icon-refresh" @click="resetData()" size="medium">清空</el-button>
      <el-button type="success" icon="el-icon-plus" @click="addParent()" size="medium">新增一级分类</el-button>
    </el-form>

    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="数据加载中"
      border
      :default-expand-all="false"
      row-key="id"
      fit
      highlight-current-row>

      <el-table-column
        type="index"
        label="序号"
        align="center"
        width="70">
      </el-table-column>

      <el-table-column prop="name" label="分类名称"/>

      <el-table-column prop="sort" label="排序" align="center" width="100"/>

      <el-table-column label="操作" align="center" width="400">
        <template slot-scope="scope">
          <el-button :disabled="scope.row.parent === '0' ? false : true" type="primary" size="mini" icon="el-icon-plus" @click="handleAddCategory(scope.row)">添加分类</el-button>
          <el-button type="success" size="mini" icon="el-icon-edit" @click="handleEdit(scope.row)">修改分类</el-button>
          <el-button type="danger" size="mini" icon="el-icon-delete" @click="handleDelete(scope.row.id)">删除分类</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      :current-page="page"
      :page-size="limit"
      :total="total"
      style="padding: 30px 0; text-align: center;"
      layout="total, prev, pager, next, jumper"
      @current-change="fetchData"
    />

    <!-- 新增修改课程分类dialog -->
    <Form
      ref="form"
      @onSaveSuccess="fetchData" />

  </div>
</template>

<script>
  import Form from '@/views/category/form'
  import { getPageList,deleteCategory } from '@/api/category'

  export default {
    data() {
      return {
        listLoading: true, // 是否显示loading信息
        list: [], // 数据列表
        total: 0, // 总记录数
        page: 1, // 页码
        limit: 10, // 每页记录数
        searchParams: {},// 查询条件
      }
    },
    methods: {

      fetchData(page = 1){
        // 当点击分页组件的切换按钮的时候，会传输一个当前页码的参数page
        this.page = page
        this.listLoading = true
        getPageList(this.page, this.limit, this.searchParams)
          .then(result => {
            if (result.success == true) {
              this.list = result.data.rows
              this.total = result.data.total
            }
            this.listLoading = false
          })
      },

      resetData(){
        this.searchParams = {}
        this.fetchData()
      },

      //新增一级分类
      addParent(){
        this.$refs.form.open({parent: '0'})
      },

      //新增二级分类
      handleAddCategory(item){
        this.$refs.form.open({parent: item.id})
      },

      //一级/二级分类编辑
      handleEdit(item){
        this.$refs.form.open(item)
      },

      //删除分类
      handleDelete(id){
        this.$confirm('此操作将永久删除该记录, 是否继续？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          return deleteCategory(id)
        }).then(result => { // 如果上一个then成功则执行此处的then回调
          if (result.code === 20000){
            this.fetchData()
            this.$message({
              type: 'success',
              message: '删除成功！'
            })
          }
        }).catch((response) => {
          if (response === 'cancel') {
            this.$message({
              type: 'info',
              message: '已取消删除！'
            })
          }
        })
      }

    },

    created() {
      this.fetchData()
    },

    components:{
      Form
    }
  }
</script>

<style scoped>
  .tree-container {
    margin-top: 20px;
    margin-left: 15px;
  }
</style>
